<?php
/**
 * 在线订单索引文件
 *
 * @author		Arthur(ArthurXF@gmail.com)
 * @copyright	(c) 2006 by bizeway.com
 * @version		$Id$
 * @package		ArthurXF
 * @subpackage	deal
 */
require_once('../product/config/config.inc.php');
require_once("../product/class/product.class.php");

$objWebInit = new product();
//数据库连接参数
$objWebInit->setDBG($arrGPdoDB);
//smarty参数
$objWebInit->arrGSmarty = $arrGSmarty;
//翻页参数
$objWebInit->arrGPage = $arrGPage;
$objWebInit->db();

//清空所有在线订单记录
if($_GET['action']=='delall'){
	unset($_SESSION['cart']);
	session_unregister('cart');
}
//删除单条在线订单记录或者在线订单数量重置
if(!empty($_POST)&&!empty($_SESSION['cart'])){
	foreach($_SESSION['cart'] as $k=>$v){
		if($_POST['del']!=''){
			if($v['id'] == $_POST['del']) {
				unset($_SESSION['cart'][$k]);
				$intID = 0;
			}
		}else{
			$_SESSION['cart'][$k]['count'] = $_POST['count'][$k];
			$_SESSION['cart'][$k]['deal_price'] = $_SESSION['cart'][$k]['my_price']*$_SESSION['cart'][$k]['count'];
			$intID = 0;
		}
	}
}

//初始化
if(!session_is_registered('cart')){
	session_register($cart);
	$_SESSION['cart'] = array();
}
$intID = intval($_GET['id']);
//如id相同则增加数量
if(!empty($_SESSION['cart'])){
	foreach($_SESSION['cart'] as $k=>$v){
		if($v['id'] == $intID) {
			$_SESSION['cart'][$k]['count']++;
			$_SESSION['cart'][$k]['deal_price'] = $_SESSION['cart'][$k]['my_price']*$_SESSION['cart'][$k]['count'];
			$intID = 0;
		}
	}
}

//无相同id的新增一条在线订单记录
if(!empty($intID)){
	$arrInfo = $objWebInit->getInfo($intID);
	if(!empty($arrInfo['my_promotion'])){
		$arrInfo['my_price'] = $arrInfo['my_promotion'];
	}
	$_SESSION['cart'][] = array('id'=>$arrInfo['id'],'title'=>$arrInfo['title'],'model'=>$arrInfo['model'],'brand'=>$arrInfo['brand'],'my_price'=>$arrInfo['my_price'],'count'=>1,'deal_price'=>$arrInfo['my_price']);
}

//print_r($arrInfo);
//重新对记录排序，便于在线订单数量的修改
sort($_SESSION['cart']);
reset($_SESSION['cart']);

//计算总金额
$intTotal = 0;
foreach($_SESSION['cart'] as $k=>$v){
	$intTotal = $intTotal+$_SESSION['cart'][$k]['deal_price'];
}

//全站公用block
@include '../_block.php';
/*

*/

//print_r($_SESSION);
//输出到模板
$arrMOutput['smarty_assign']['Title'] = '购物清单 - '.$arrGWeb['name'];
$arrMOutput['smarty_assign']['Total'] = $intTotal;
$arrMOutput["smarty_assign"]['arrData'] = $_SESSION['cart'];

$arrMOutput["smarty_assign"]['MAIN'] = $arrGSmarty['main_dir'].'../deal/filluserinfo.html';
$objWebInit->output($arrMOutput);
?>